<template>
    <div class="container">
        <div class="user-info">
            <div class="user-avater" @click="onUpdateAvatar">
                <img
                    :src="
                        userStore.avatar
                            ? userStore.avatar
                            : _oss_ + 'images/user/default-avatar.png'
                    "
                />
                <input
                    type="file"
                    accept="image/*"
                    style="display: none"
                    ref="avatarInputRef"
                    @change="onAvatarChange"
                />

                <div class="icon-camera-mask">
                    <img :src="`${_oss_}images/admin/icon-camera.png`" />
                </div>
            </div>

            <div class="user-phone">{{ mobile }}</div>

            <div class="mid-box">
                <div class="mid-item" @click="GoToScoreList()">
                    <div class="mid-item__top">
                        {{
                            userStore.midNum.score_num > 999
                                ? "999+"
                                : userStore.midNum.score_num || 0
                        }}
                    </div>
                    <div class="mid-item__bottom">积分</div>
                </div>
                <div class="mid-item" @click="GoToMidList(0)">
                    <div class="mid-item__top">
                        {{
                            userStore.midNum.like_num +
                                userStore.midNum.case_like_num || 0
                        }}
                    </div>
                    <div class="mid-item__bottom">点赞</div>
                </div>
                <div class="mid-item" @click="GoToMidList(1)">
                    <div class="mid-item__top">
                        {{
                            userStore.midNum.star_num +
                                userStore.midNum.case_star_num || 0
                        }}
                    </div>
                    <div class="mid-item__bottom">收藏</div>
                </div>
            </div>
        </div>

        <div class="tab-list">
            <div class="tab-group">
                <template v-for="item in navList" :key="item.name">
                    <div
                        class="tab-item"
                        :class="{
                            active: route.path === item.path,
                        }"
                        @click="jumpPath(item.path)"
                        v-if="item.show"
                    >
                        {{ item.name }}
                    </div>
                </template>
            </div>
        </div>

        <slot />
        <DialogAvatarCropperM ref="avatarCropperRef" />
        <LazyDialogAvatarSelectM ref="avatarDialogRef" @diy="onDiy" />
    </div>
</template>
<script setup>
import { useContent } from ".";
import { useMobile } from "@/hooks";

const route = useRoute();
const mobile = useMobile();
const userStore = useUserStore();
const dialogStore = useDialogStore();
const avatarCropperRef = ref(null);
const avatarInputRef = ref(null);
const avatarDialogRef = ref(null);
const { navList } = useContent();

const onAvatarChange = (e) => {
    const file = e.target.files[0];
    avatarCropperRef.value.show(window.URL.createObjectURL(file), file.name);
    e.target.value = "";
};

const onUpdateAvatar = () => {
    avatarDialogRef.value.show();
};

const jumpPath = (path) => navigateTo(path);

const GoToMidList = (type) => {
    dialogStore.isMidListDialogVisible = true;
    dialogStore.midListType = type;
};

const GoToScoreList = () => {
    dialogStore.isTaskListDialogVisible = true;
};

const onDiy = () => {
    avatarInputRef.value.click();
};
</script>
<style lang="scss" scoped>
.container {
    min-height: calc(100vh - 44px);
    background-color: #f5f5f5;

    .user-info {
        background-color: #fff;
        // height: 130px;
        text-align: center;

        .user-avater {
            position: relative;
            width: 75px;
            height: 75px;
            margin: 15px auto 0;
            font-size: 0;
            overflow: hidden;
            border-radius: 50%;

            img {
                width: 100%;
                height: 100%;
            }

            .icon-camera-mask {
                position: absolute;
                left: 0;
                top: 0;
                width: 100%;
                height: 100%;
                background: rgba($color: #000000, $alpha: 0.5);
                display: flex;
                align-items: center;
                justify-content: center;

                img {
                    width: 20px;
                    height: 20px;
                }
            }
        }

        .user-phone {
            margin-top: 12px;
            font-size: 14px;
            color: #333;
        }

        .mid-box {
            display: flex;
            justify-content: center;
            padding: 15px 0;

            .mid-item {
                width: 80px;
                text-align: center;

                &__top {
                    font-size: 18px;
                    font-weight: bold;
                }

                &__bottom {
                    font-size: 12px;
                    color: #888;
                    margin-top: 6px;
                }
            }
        }
    }

    .tab-list {
        position: relative;
        width: calc(100% - 32px);
        height: 1px;
        padding: 20px 0 25px;
        margin: 0 auto 25px;

        &::after {
            position: absolute;
            left: 0px;
            bottom: -3px;
            display: block;
            width: 100%;
            height: 1px;
            background-color: #5085fb;
            opacity: 0.2;
            content: "";
        }

        .tab-group {
            position: relative;
            white-space: nowrap;
            overflow-x: auto;

            &::-webkit-scrollbar {
                display: none;
            }
        }

        .tab-item {
            display: inline-block;
            margin-right: 40px;
            color: #666;
            font-size: 14px;
            padding-bottom: 10px;

            &.active {
                position: relative;
                color: #5085fb;
                font-weight: bold;

                &::after {
                    position: absolute;
                    width: 100%;
                    height: 1px;
                    left: 0px;
                    bottom: 0px;
                    content: "";
                    display: block;
                    background-color: #5085fb;
                }
            }

            &:last-of-type {
                margin-right: 0;
            }
        }
    }
}
</style>
